import 'package:flutter/material.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import '../../helper/cacheHelper.dart';
import '../../pages/station/station_helper.dart';
import '../../widgets/IconWidget.dart';
import '../../widgets/inputWidget.dart';

import '../../model/station.dart';
import '../../model/stationequip.dart';

class StationEquipAddPage extends StatefulWidget {
  @override
  StationEquipAddPage(this.s1, this.station1, this.added);
  final StationEquip s1;
  final Station station1;
  final VoidCallback added;
  @override
  StationEquipAddPageState createState() => new StationEquipAddPageState();
}

class StationEquipAddPageState extends State<StationEquipAddPage> {
  late StationEquip X1;
  late List<String> TypeList;
  bool ifLoaded = false;

  final TextEditingController c1 = new TextEditingController();
  final TextEditingController c2 = new TextEditingController();
  final TextEditingController c3 = new TextEditingController();
  final TextEditingController c4 = new TextEditingController();
  final TextEditingController c5 = new TextEditingController();
  final TextEditingController c6 = new TextEditingController();
  final TextEditingController c7 = new TextEditingController();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    X1 = widget.s1;

    c1.text = X1.Name;
    // c2.text = X1.EState;
    c3.text = X1.Model;
    c4.text = X1.FacturerName;
    c5.text = X1.FactureTime;
    c6.text = X1.Remark;
    loadData();
  }

  loadData() async {
    //  print("TypeList");
    TypeList = await StationEquip.GetTypeList();
    // print(TypeList);
    setState(() {
      ifLoaded = true;
    });
  }

  upload(BuildContext context) async {
    //X1.Name = c1.text;
    // X1.EState = c2.text;
    X1.Model = c3.text;
    X1.FacturerName = c4.text;
    X1.FactureTime = c5.text;
    X1.Remark = c6.text;

    if (X1.Name == "") X1.Name = TypeList[0];

    if (X1.EState == "") X1.EState = "正常";

    if (X1.ID == 0) {
      await StationEquip.Add(X1);
    } else {
      await StationEquip.Update(X1);
    }

    // widget.added();
    Navigator.of(context).pop();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      //backgroundColor: Colors.white,
      appBar: new AppBar(
        // title: bottomTabs[currentIndex].title,
        title: Text("设备编辑"),

        // leading: Icon(Icons.menu),
      ),
      body: StationEquipAddBody(context),
    );
  }

  void onNameChange(dynamic nn) {
    String yy = nn.toString();
    setState(() {
      X1.Name = yy;
    });
  }

  void onEStateChange(dynamic nn) {
    String yy = nn.toString();
    setState(() {
      X1.EState = yy;
    });
  }

  List<Widget> getWidgetList(BuildContext context) {
    List<Widget> list1 = <Widget>[];
    if (TypeList == null) return list1;

    List<Widget> list2 = <Widget>[];
    List<Widget> list3 = <Widget>[];

    list1.add(InputWidget.inCard(
      ListTile(
        leading: IconWidget.RiverImage(),
        title: Text(
          widget.station1.STNM,
        ),
      ),
    ));

    //list2.add(InputWidget.getInput("设备名称", c1));
    list2.add(
      InputWidget.getSelect(
          "设备名称", TypeList, X1.Name == null ? TypeList[0] : X1.Name,
          onChanged: onNameChange),
    );
    list2.add(Divider(
      height: 0.0,
    ));
    // list2.add(InputWidget.getInput("设备状态", c2));
    list2.add(
      InputWidget.getSelect("设备状态", StationHelper.StateList(), X1.EState,
          onChanged: onEStateChange),
    );

    list2.add(Divider(
      height: 0.0,
    ));
    list2.add(InputWidget.getInput("设备型号", c3));
    list2.add(Divider(
      height: 0.0,
    ));
    list2.add(InputWidget.getInput("生产厂家", c4));
    list2.add(Divider(
      height: 0.0,
    ));

    list2.add(InputWidget.getInput("生产日期", c5));
    list2.add(Divider(
      height: 0.0,
    ));
    list2.add(InputWidget.getInput2("备注", c6));
    list2.add(Divider(
      height: 0.0,
    ));
    list1.add(InputWidget.inCard2(list2));

    list1.add(
        InputWidget.inCard(InputWidget.getButton("保存", () => upload(context))));
    //list1.add(getCLWidget(s1.State==6, Helper.DateTimeFormat(s1.HZQMTM), s1.HZQM));
    return list1;
  }

  StationEquipAddBody(BuildContext context) => SingleChildScrollView(
        child: StationEquipAddFields(context),
      );

  StationEquipAddFields(BuildContext context) {
    if (!ifLoaded)
      return Center(
          child: SpinKitCircle(
        color: Colors.blue,
      ));
    return Container(child: Column(children: getWidgetList(context)));
  }
}
